package com.xayy.health.mgt.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.BeanUtil;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.xayy.health.mgt.model.HealthAssessTask;
import com.xayy.health.mgt.service.HealthAssessTaskService;
import org.springframework.stereotype.Service;

/**
 * @author Nie
 * @version V1.0 @ Date 2024/6/6 14:29
 */
@Service
public class HealthAssessTaskServiceImpl extends BaseServiceImpl<HealthAssessTask> implements HealthAssessTaskService {
    @Override
    public CheckResult<Object> getSatTaskInfo(Long satelliteMid) {
        HealthAssessTask healthAssessTask = selectOne(HealthAssessTask.builder().satelliteMid(satelliteMid).build());
        if (BeanUtil.isEmpty(healthAssessTask)) {
            healthAssessTask = new HealthAssessTask();
            healthAssessTask.setSatelliteMid(satelliteMid);
            insert(healthAssessTask);
        }
        return CheckResult.success(healthAssessTask);
    }

    @Override
    public CheckResult<Object> healthAutoRunTask(HealthAssessTask healthAssessTask) {
        if (healthAssessTask.getTaskCurrentTime() == null) {
            healthAssessTask.setTaskCurrentTime(healthAssessTask.getTaskStartTime());
        }

        HealthAssessTask healthAssessTaskDb = selectById(healthAssessTask.getId());
        if (healthAssessTaskDb.getTaskStartTime() != healthAssessTask.getTaskStartTime()) {
            healthAssessTask.setTaskCurrentTime(healthAssessTask.getTaskStartTime());
            healthAssessTask.setLastTaskTime(DateUtil.date());
        }

        updateById(healthAssessTask);
        return CheckResult.success();
    }
}
